<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class VillagePublishCheck extends Model
{
    use SoftDeletes;
    protected $table = 'village_publish_check';

    /**
     * 定义主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 禁止批量赋值的字段
     *
     * @var array
     */
    protected $guarded = ['id','created_at','deleted_at'];

    /**
     * 软删除应该被调整为日期的属性
     * [$dates description]
     * @var [type]
     */
    protected $dates=[
        'created_at', 'updated_at','deleted_at'
    ];

    /**
     * 乡村在线审核
     * @param array
     * @return bool
     */
    public static function store($param)
    {

        DB::beginTransaction();
        // 拒绝理由为空
        if(!$param['refuse_reason']){ //通过

            $param['status'] = 1;
            $param['is_check_admin'] = $param['is_admin'] ?? 0;
            $res = self::create($param);

            $res2 = VillagePublish::where('id',$param['pub_id']) -> update(['check_status' => 2,'passed_at' => date('Y-m-d H:i:s',time())]);
        }else{                        // 不通过
            $param['status'] = 0;
            $param['is_check_admin'] = $param['is_admin'] ?? 0;
            $res = self::create($param);

            $res2 = VillagePublish::where('id',$param['pub_id']) -> update(['check_status' => 1]);
        }

        if($res && $res2){

            DB::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }



    }
}
